package com.sywb.chuangyebao.view.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.contract.QAndATypeContract;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.view.AskActivity;
import com.sywb.chuangyebao.view.SearchActivity;
import com.sywb.chuangyebao.widget.ColorTrackTabLayout;

import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;

/**
 * 问答
 */
public class QAndAFragment extends BaseStatisticsFragment<QAndATypeContract.Presenter> implements QAndATypeContract.View {

    TextView actionbarSerach;
    TextView actionbarAsk;
    ColorTrackTabLayout qandaType;
    ViewPager qandaViewpager;

    TextView tvAsk;

    /**
     * Fragment 实例
     */
    public static QAndAFragment newInstance(Object... pramars) {
        QAndAFragment fragment = new QAndAFragment();
        fragment.setArguments(getBundle(pramars));
        return fragment;
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.fragment_qanda;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        super.initView(view, bundle);
        actionbarSerach = getView(R.id.actionbar_serach);
        actionbarAsk = getView(R.id.actionbar_ask);
        qandaType = getView(R.id.qanda_type);
        qandaViewpager = getView(R.id.qanda_viewpager);
        tvAsk = getView(R.id.tv_ask);

        actionbarSerach.setOnClickListener(this);
        actionbarAsk.setOnClickListener(this);
        tvAsk.setOnClickListener(this);
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
    }

    @Override
    public void onClick(View view) {
        if (!isCanClick(view)) return;
        switch (view.getId()) {
            case R.id.actionbar_serach:
                advance(SearchActivity.class);
                break;
            case R.id.actionbar_ask:
            case R.id.tv_ask:
                if (Utils.checkIsNext(getMyFragmentManager())) {
                    advance(AskActivity.class, AskActivity.COMING_TYPE_ASK);
                }
                break;
        }
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.POST_LIST_CATEGORY)})
    public void rxBack(String data) {
        Logger.e("切换分类：" + data);
        mPresenter.changeCategory(data);
    }

    @Override
    public boolean useRxBus() {
        return true;
    }

    @Override
    public FragmentManager getUserFragmentManager() {
        return getChildFragmentManager();
    }

    @Override
    public ColorTrackTabLayout getTabLayout() {
        return qandaType;
    }

    @Override
    public ViewPager getViewpager() {
        return qandaViewpager;
    }

    @Override
    public void initCategorieError() {
    }

}
